package tx.audioAndVideoRecorder;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class VideoActivity extends Activity implements SurfaceHolder.Callback{
		
		private static final String TAG = "VideoActivity";
		
		private SurfaceView surfaceView;
		private SurfaceHolder surfaceHolder;
		private Camera camera;
		private boolean previewRunning;
		private boolean recording = false;
		
		private MyVideoRecorder mVideoRecorder = null;		
		
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	            super.onCreate(savedInstanceState);
	            setContentView(R.layout.tab_video);
	            surfaceView = (SurfaceView) findViewById(R.id.surface_cam);
	            try {
	            	 surfaceHolder = surfaceView.getHolder();
				} catch (Exception e) {
					Log.e(TAG,e.getMessage());
				}
	            surfaceHolder.addCallback(this);
	            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	            
	            setButtonHandlers();
	    }
		
		private void setButtonHandlers() {
				((Button)findViewById(R.id.btnVideoRecord)).setOnClickListener(btnClick);
		}
		
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			camera = Camera.open();
			if (camera != null){
				Camera.Parameters params = camera.getParameters();
				camera.setParameters(params);
				
				// video recorder is created here
				mVideoRecorder = new MyVideoRecorder(surfaceHolder,camera);
			}
			else {
				Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
				finish();
			}
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			if (previewRunning){
				camera.stopPreview();
			}
			Camera.Parameters p = camera.getParameters();
			//p.setPreviewSize(width, height);
			p.setPreviewSize(240, 160);
			p.setPreviewFormat(PixelFormat.JPEG);
			camera.setParameters(p);

			try {
				camera.setPreviewDisplay(holder);
				camera.startPreview();
				previewRunning = true;
			}
			catch (IOException e) {
				Log.e(TAG,e.getMessage());
				e.printStackTrace();
			}
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			camera.stopPreview();
			previewRunning = false;
			camera.release();
		}	

		
		private View.OnClickListener btnClick = new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	                switch(v.getId()){
	                        case R.id.btnVideoRecord:{
	                        	                    	
	                    	if(recording){
	                    		mVideoRecorder.stopRecording();
	                    		Toast.makeText(getApplicationContext(), "STOP RECORDVIDEO", 1000).show();
	                    		recording = !recording;
	                    	}else
	                    	{
	                    		// We are testing if startRecording is working
	                    		if(mVideoRecorder.startRecording()){
	                    			Toast.makeText(getApplicationContext(), "RECORDVIDEO", 1000).show();
	                    			recording = !recording;
	                    		}
	                    		
	                    	}
	                        break;
	                }
	                }
	        }
	}; 
}

